package skitur.server;

import java.util.ArrayList;
import java.util.Date;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;

import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

class Region{
	public Region(int x, int y, String name) {
		super();
		this.x = x;
		this.y = y;
		this.name = name;
		//78,249
	}
	public String getSvg(){
		return "<g ><path  d=\"m" + x + "," + y + "c0,1 0.69255,0.814 2,-1c0.8269,-1.14726 2.34619,-0.70546 4,-3c1.30745,-1.814 2.71412,-3.21167 5,-6c0.8966,-1.09367 3.20681,-2.81265 5,-5c2.28588,-2.78833 3.186,-4.69255 5,-6c1.14727,-0.8269 3,-2 4,-3c2,-2 2.71412,-4.21167 5,-7c1.79319,-2.18735 3.186,-1.69255 5,-3c1.14727,-0.8269 2.186,-0.69255 4,-2c1.14727,-0.8269 2.02675,-0.77025 3,-1c2.17625,-0.51375 4.02583,-1.67964 6,-2c3.12144,-0.50655 4,-1 6,-1c2,0 4,0 7,0c1,0 4,0 5,0c2,0 4,0 6,0c3,0 4,0 5,0c2,0 4.02675,-0.22975 5,0c2.17625,0.51375 5,1 7,1c3,0 4,1 7,1c2,0 4,0 6,0c1,0 3.0535,0.5405 5,1c2.17625,0.51375 4,1 5,1c1,0 2,0 3,0c1,0 2.07613,0.61731 3,1c1.30656,0.5412 1.82375,1.48625 4,2c0.97325,0.22975 3.02675,1.77025 4,2c2.17625,0.51375 4.186,1.69255 6,3c1.14726,0.8269 3.02675,1.77025 4,2c2.17625,0.51375 2.29289,1.29289 3,2c0.70711,0.70711 0.82375,1.48625 3,2c0.97325,0.22975 1.82375,2.48625 4,3c0.97325,0.22975 2,1 3,2c1,1 1.61731,2.07613 2,3c0.5412,1.30656 3.1731,1.85274 4,3c1.30745,1.814 1.69255,3.186 3,5c0.8269,1.14726 2.4588,1.69344 3,3c0.38269,0.92387 0.77025,2.02675 1,3c0.51375,2.17625 2,3 4,5c1,1 2,3 3,4l0,1l1,0l0,1\" id=\"svg_1\" stroke-width=\"5\" stroke=\"#000000\" fill=\"none\"/>"
		 + "<path  d=\"m" + (x + 135) + "," + (y-31) + "c1,0 2.07613,0.38269 3,0c2.61313,-1.0824 2.88152,-3.19028 6,-5c1.93399,-1.12234 3.15927,-1.61064 6,-3c3.23892,-1.58411 5,-4 8,-5c3,-1 3.82375,-2.48625 6,-3c0.97325,-0.22975 2.15224,-0.23463 4,-1c1.30656,-0.5412 2,-1 3,-1c1,0 2,0 3,0c2,0 4,0 5,0c1,0 2,0 3,0c1,0 2,0 3,0c1,0 1.69345,0.4588 3,1c0.92389,0.38269 2.02676,0.77025 3,1c2.17624,0.51375 2.69345,1.4588 4,2c0.92389,0.38269 2.07611,0.61731 3,1c1.30655,0.5412 3,1 4,1c1,0 1.69345,0.4588 3,1c0.92389,0.38269 1.85272,1.1731 3,2c1.814,1.30745 4,1 5,2c1,1 1.69345,1.4588 3,2c0.92389,0.38269 1.07611,0.61731 2,1c1.30655,0.5412 2.07611,1.61731 3,2c1.30655,0.5412 1.29291,1.29289 2,2c0.70709,0.70711 2.4588,-0.30656 3,1c0.38269,0.92387 1,2 2,3c1,1 2,2 3,3c1,1 1.29291,1.29289 2,2c0.70709,0.70711 1.77023,1.02675 2,2c0.51373,2.17625 2,3 3,4c1,1 1.29291,1.29289 2,2c0.70709,0.70711 1.4588,1.69344 2,3c0.38269,0.92387 1.4588,0.69344 2,2c0.38269,0.92387 1,2 1,3l1,1l1,1\" id=\"svg_2\" stroke-width=\"5\" stroke=\"#000000\" fill=\"none\"/>"
		 + "<text  xml:space=\"preserve\" font-family=\"serif\" font-size=\"24\" id=\"svg_15\" y=\"" + y + "\" x=\"" + (x + 50) + "\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-width=\"0\" stroke=\"#000000\" fill=\"#000000\">" + name + " : " + conditions + "</text></g>";
	}

	int x, y;
	String name;
	long conditions;
	
}
public class ConditionsMap{
	ArrayList<Region> regions = new ArrayList<Region>();

	public ConditionsMap() {
		regions.add(new Region(800, 300, "Tatry"));
		regions.add(new Region(800, 200, "Gorce"));
		regions.add(new Region(400, 300, "Babia"));
		regions.add(new Region(300, 200, "Pilsko"));
		regions.add(new Region(50, 200, "Karkonosze"));
	}

	public String getMap() {
	

		// ...
		// Get the Datastore Service
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		// The Query interface assembles a query
		Query q = new Query("Conditions");
		//q.addFilter("lastName", Query.FilterOperator.EQUAL, lastNameParam);
		//q.addFilter("height", Query.FilterOperator.LESS_THAN, maxHeightParam);

		// PreparedQuery contains the methods for fetching query results
		// from the datastore
		PreparedQuery pq = datastore.prepare(q);

		
		for (Entity result : pq.asIterable()) {
			  String region = (String) result.getProperty("region");
			  Long conditions = (Long) result.getProperty("conditions");
			  for(Region r : regions){
				  if(r.name.compareTo(region) == 0){
					  r.conditions = conditions;
				  }
				}
			}
		
		
		String svgRet ="<svg width=\"1200\" height=\"600\" xmlns=\"http://www.w3.org/2000/svg\">"; 
		for(Region r : regions){
			svgRet += r.getSvg();
		}
		return svgRet + "</svg>"; 
	}
}
